angular.module("NTApp").controller('DeviceDetailCtrl', function($state, $stateParams, $interval, WXService, DeviceService) {
  var d = this;
  var openId = WXService.getWeChat().openId;
  var idStr = $stateParams.idStr
  var deviceId = idStr.split('-')[0];
  var pId = idStr.split('-')[1];
  var deviceIdx = DeviceService.getSelected();
  var deviceInfo = DeviceService.getDevicesInfo()[deviceIdx];
  var profix = '00000000';

  WXService.setListToDetail(false);
  d.currentTemp = deviceInfo.expectedTemp || '0';
  d.editable = !deviceInfo.editable;
  d.isOn = deviceInfo.isOn;
  d.showContactDetail = false;
  d.showLoading = false;
  d.showWarning = false;

  var g1 = new JustGage({
    id: "g1",
    value: d.currentTemp,
    min: 5,
    max: 35,
    label: "度",
    gaugeWidthScale: 0.5
  });

  refresh();

  function refresh() {
    d.timer = $interval(function() {
      deviceInfo = DeviceService.getDevicesInfo()[deviceIdx];
      d.currentTemp = deviceInfo.expectedTemp || '0';
      d.editable = !deviceInfo.editable;
      d.isOn = deviceInfo.isOn;
      console.log(deviceInfo);
    }, 1000 * 2);
  }

  d.contactClicked = function() {
    if (deviceInfo.userId == deviceInfo.owner) {
      d.showContactDetail = !d.showContactDetail;
    } else {
      $state.go('ContactUs');
    }
  };

  // d.changeTemp = function(type) {
  //   if (!deviceInfo.isOn) {
  //     alert('请先打开设备');
  //     return;
  //   }
  //   d.currentTemp = parseInt(d.currentTemp);
  //
  //   if (d.currentTemp == 35 && type == '+') {
  //     alert('温度不能超过35摄氏度');
  //     return;
  //   }
  //
  //   if (d.currentTemp == 5 && type == '-') {
  //     alert('温度不能低于5摄氏度');
  //     return;
  //   }
  //
  //   $interval.cancel(d.timer);
  //
  //   type === '+' ? d.currentTemp++ : d.currentTemp--;
  //   var command = profix + getCommand('temp');
  //   tempChange('65', command);
  // }



  // start



  d.changeTemp = function() {
    if (!deviceInfo.isOn) {
      alert('请先打开设备');
      return;
    }

    d.currentTemp = parseInt(d.currentTemp);
    $interval.cancel(d.timer);
    g1.refresh(d.currentTemp);
    var command = profix + getCommand('temp');
    tempChange('65', command);
  }
  // end

  d.powerControl = function(type) {

    if (type == 'on' && deviceInfo.isOn) {
      alert('设备已经开机');
      return;
    }
    if (type == 'off' && !deviceInfo.isOn) {
      alert('设备未开机');
      return;
    }

    var command = type == 'on' ? profix + getCommand('on') : profix + getCommand('off');
    switchChange('65', command, type);
  }

  function getCommand(type) {
    var front = '0301';
    switch (type) {
      case 'on':
        return '0f010300010000000000';
      case 'off':
        return '0f010300020000000000';
      case 'temp':
        return '0f01' + front + getValue() + '0000000000';
      default:
        return;
    }
  }

  function switchChange(messageCode, sendData, type) {
    d.showLoading = true;
    DeviceService.setDevice(openId, sendData, pId, deviceId, messageCode).then(function(result) {
      d.showLoading = false;
      var deStr = DeviceService.base64decode(result);
      var hexStr = DeviceService.CharToHex(deStr);
      var arr = hexStr.split(',');
      if (arr[1] == 'f' && (arr[3] == 1 || arr[3] == 3)) {
        alert(type == 'on' ? '设备启动成功' : '设备关闭成功');
        $interval.cancel(d.timer);
        $state.go('DevicesList');
      } else {
        alert('设备离线，请检查网络');
      }
    }, function() {
      d.showLoading = false;
      alert('设备离线，请检查网络');
    });
  }

  function getValue() {
    var value = d.currentTemp - 5;
    value = value.toString(16);
    if (value.length == 1) {
      value = '0' + value;
    }
    return value;
  }

  function tempChange(messageCode, sendData) {
    d.showLoading = true;
    DeviceService.setDevice(openId, sendData, pId, deviceId, messageCode).then(function(result) {
      d.showLoading = false;
      var deStr = DeviceService.base64decode(result);
      var hexStr = DeviceService.CharToHex(deStr);
      var arr = hexStr.split(',');

      if (arr[1] != 'f' && !arr[3] != 1) {
        alert('设置温度失败');
      }
    }, function(error) {
      d.showLoading = false;
      console.log('wrong->', error);
      alert('设置温度失败');
    });
  }

  d.options = function(path) {
    if (path == 'ShareDevice') {
      $state.go(path, {
        idStr: idStr
      });
    } else {
      $state.go(path);
    }
  }

  d.showRemoveWarning = function() {
    d.showWarning = true;
  }

  d.hideRemoveWarning = function() {
    d.showWarning = false;
  }

  d.removeDevice = function() {
    d.hideRemoveWarning();
    DeviceService.unbinding(openId, pId).then(function(result) {
      if (result) {
        window.history.back(-1);
        $interval.cancel(d.timer);
        alert('设备解绑成功');
      } else {
        alert('设备解绑失败');
      }
    }, function() {
      alert('设备解绑失败');
    });
  }

  window.onbeforeunload = function() {
    $interval.cancel(d.timer);
    $state.go('DevicesList');
  }
});
